import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/component/member_header.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/model/point_model.dart';
import 'package:village_android/model/sys_config_model.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/article/article_list.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/http_utlis.dart';
import 'package:intl/intl.dart';

class HealthPointPage extends StatefulWidget {
  @override
  _HealthPointPageState createState() => _HealthPointPageState();
}
//NewsCell

class _HealthPointPageState extends State<HealthPointPage> {
  List items = [];
  UserModel userModel = CommonUtils.getLocalUserInfo();

  var _totalPoint = 100;
  var _mouthPoint = 0;
  var _yearPoint = 0;
  var _leftPoint = 100;

  @override
  void initState() {
    super.initState();
    getData();
    getStatData();
  }

  void getData() {
    HttpUtils.get("/api/point/list", data: {}, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  void getStatData() {

    DateTime now = DateTime.now();
    String formattedDate = DateFormat('yyyy-MM-dd').format(now);
    HttpUtils.get("/api/point/stats", data: {"reportDate":formattedDate}, success: (data) {

      if(data!=null) {
        List items = data as List;
        for(var i=0;i<items.length;i++){
          var item = items[i];
          if(item["field"] == "mouth") {
            _mouthPoint = int.parse(item["value"]);
          }
          if(item["field"] == "year") {
            _yearPoint = int.parse(item["value"]);
          }
        }
      }
      setState(() {
        _leftPoint = _totalPoint - _yearPoint;
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('卫生积分管理'),
          actions: <Widget>[
            new RaisedButton(
                color: Colors.red,
                child: Text(
                  "积分制度",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  MenuModel menuModel = MenuModel("积分制度", 36, 3, "");
                  var page = new ArticleListPage(
                    menuModel: menuModel,
                  );
                  Navigator.push(context,
                      new MaterialPageRoute(builder: (context) => page));
                }),
          ],
        ),
        body:Center(
                child: Container(
                  child: ListView.separated(
                      itemBuilder: (context, index) {
                        if (index == 0) {
                          // return the header
                          return new Column(
                            children: <Widget>[
                              userModel != null
                                  ? MemberHeader(
                                      userModel: userModel,
                                    )
                                  : SizedBox(height: 1),
                              totalPointDesc(),
                              totalPoint(),
                              Divider(height: 20.0, color: Colors.black12),
                              items.isEmpty
                                  ? Center(child: Text('无数据'))
                                  :  header(),
                            ],
                          );
                        } else {

                          index -= 1;
                          PointModel pointModel =
                              PointModel.fromJson(items[index]);
                          return _newsCell(context, pointModel);
                        }
                      },
                      separatorBuilder: (context, index) =>
                          Divider(height: 1.0, color: Colors.black12),
                      itemCount: items == null ? 1 : items.length + 1),
                ),
              ),
      ),
    );
  }

  Widget totalPointDesc() {
    return Container(
      height: ScreenUtil.getInstance().setHeight(60),
      child: ListTile(
        onTap: null,
        title: Row(children: <Widget>[
          new Expanded(
            child: new Text("本年度积分",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.black,
                )),
          ),
          new Expanded(
              child: new Text("本月扣分",
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                  ))),
          new Expanded(
              child: new Text("本年积累扣分",
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                  ))),
          new Expanded(
              child: new Text("本年剩余分数",
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                  ))),
        ]),
      ),
    );
  }

  Widget totalPoint() {
    return Container(
      height: ScreenUtil.getInstance().setHeight(60),
      child: ListTile(
        onTap: null,
        title: Row(children: <Widget>[
          new Expanded(
              child: new Text(_totalPoint.toString(),
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red,
                  ))),
          new Expanded(
              child: new Text(_mouthPoint.toString(),
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red,
                  ))),
          new Expanded(
              child: new Text(_yearPoint.toString(),
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red,
                  ))),
          new Expanded(
              child: new Text(_leftPoint.toString(),
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red,
                  ))),
        ]),
      ),
    );
  }

  Widget header() {
    return ListTile(
      onTap: null,
      title: Row(children: <Widget>[
        new Expanded(child: new Text("扣分时间")),
        new Expanded(child: new Text("原因")),
        new Expanded(child: new Text("分值")),
        new Expanded(child: new Text("执行人")),
        new Expanded(child: new Text("执行时间")),
      ]),
    );
  }

  void onItemClick(item) {
//    var page = new BrowserPage(title:item.title,url:item.url,);
//    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
  }

  //独立Item
  Widget _newsCell(BuildContext context, PointModel item) {
    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(100),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            new Expanded(
                child: new Text(
              item.createTime,
              style: TextStyle(
                fontSize: 12,
                color: Colors.black,
              ),
            )),
            Padding(padding: EdgeInsets.only(right: 2)),
            new Expanded(child: new Text(item.detail)),
            new Expanded(
                child: new Text(
              item.point.toString(),
              style: TextStyle(
                fontSize: 12,
                color: Colors.red,
              ),
            )),
            new Expanded(
                child: new Text(
                  item.executor,
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.black,
                  ),
                )),
            new Expanded(
                child: new Text(
              item.executorTime,
              style: TextStyle(
                fontSize: 12,
                color: Colors.black,
              ),
            )),
          ],
        ),
      ),
    );
  }
}
